package com.kgcx.eureka.interceptor;

import com.kgcx.eureka.config.EscConfig;
import com.kgcx.eureka.config.ServiceConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Map;

/**
 * @author 吴刚林
 * @date 2017/11/28
 */
@Component
public class InstanceInfoReplaceHandlerInterceptor implements HandlerInterceptor {
    @Autowired
    private EscConfig escConfig;
    @Autowired
    private ServiceConfig serviceConfig;
    @Value("${eureka.dashboard.path:/}")
    private String path;

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable ModelAndView modelAndView) {


        if (request.getServletPath().equals(path)) {
//            Set<Map.Entry<String, String>> replicas = (Set<Map.Entry<String, String>>) modelAndView.getModel().get("replicas");
//            if (null != replicas) {
//                replicas.forEach(i -> {
//                    if (null != escConfig.getEsc() && escConfig.getEsc().size() != 0) {
//                        escConfig.getEsc().forEach((kk, vv) -> {
//                            String innerIp = StringUtils.replace(
//                                    StringUtils.replace(kk, "IP-", "")
//                                    , "-", ".");
//                            if (i.getKey().contains(innerIp)) {
//                                replicas.remove(i);
//                                String replace = StringUtils.replace(i.getKey(), innerIp, vv);
//                                String url = StringUtils.replace(StringUtils.replace(i.getValue(), innerIp, vv), "/eureka", "");
//                                Map<String, String> ii = new LinkedHashMap<>();
//                                ii.put(replace, url);
//                                replicas.addAll(ii.entrySet());
//                            }
//                        });
//                    }
//                });
//            }
            ArrayList<Map<String, Object>> apps = (ArrayList<Map<String, Object>>) modelAndView.getModel().get("apps");
            if (!CollectionUtils.isEmpty(apps)) {
                apps.forEach(app -> {
                    String desc = serviceConfig.getService().get(app.get("name").toString().toLowerCase());
                    if (StringUtils.hasText(desc)) {
                        app.put("name", app.get("name") + "(" + desc + ")");
                    }
                    ArrayList<Map<String, Object>> instanceInfos = (ArrayList<Map<String, Object>>) app.get("instanceInfos");
                    if (!CollectionUtils.isEmpty(instanceInfos)) {
                        instanceInfos.forEach(instanceInfo -> {
                            ArrayList<Map<String, Object>> instances = (ArrayList<Map<String, Object>>) instanceInfo.get("instances");
                            if (!CollectionUtils.isEmpty(instances)) {
                                instances.forEach(instance -> {
                                    Object url = instance.get("url");
                                    if (null != url) {
                                        String urlStr = url.toString();
                                        if (null != escConfig.getEsc() && escConfig.getEsc().size() != 0) {
                                            escConfig.getEsc().forEach((k, v) -> {
                                                String innerIp = StringUtils.replace(
                                                        StringUtils.replace(k, "IP-", "")
                                                        , "-", ".");
                                                if (urlStr.contains(innerIp)) {
                                                    //各服务在注册eureka时默认取内网地址注册，
                                                    //需要在eureka控制台转换为对应的外网地址暴露，方便开发测试人员点击查看
                                                    String replace = StringUtils.replace(urlStr, innerIp, v);
                                                    instance.put("url", replace);
                                                    instance.put("id", v + "(" + innerIp + ")");
                                                }
                                            });
                                        }
                                    }
                                });
                            }
                        });
                    }
                });
            }
        }
    }
}
